package com.vip.common.response;

import com.vip.common.enums.ResponseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author ASUS
 * @Date 2025/3/3 14:01
 * @Descripition
 */
@Schema(description = "通用返回对象")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse {
    @Schema(description = "状态码")
    private Integer code; // 状态码
    @Schema(description = "返回信息")
    private String message;
    @Schema(description = "返回数据")
    private Object data;

    /**
     * 操作成功，返回空数据
     */
    public static CommonResponse success() {
        return new CommonResponse(200, "操作成功", null);
    }

    /*
     * 操作成功，返回响应信息
     */
    public static CommonResponse success(String message) {
        return new CommonResponse(200, message, null);
    }

    /**
     * 操作成功 ，返回响应数据静态方法
     *
     * @param data 响应数据
     */
    public static CommonResponse success(Object data) {
        return new CommonResponse(200, "操作成功", data);
    }

    /**
     * 操作失败，返回错误信息
     */
    public static CommonResponse error(ResponseEnum responseEnum) {
        return new CommonResponse(responseEnum.getCode(), responseEnum.getMsg(), null);
    }

    /*
     * 操作失败，返回错误信息
     */
    public static CommonResponse error(ResponseEnum responseEnum, String msg) {
        return new CommonResponse(responseEnum.getCode(), msg, null);
    }

    /*
     * 操作失败，返回错误信息
     */
    public static CommonResponse error(ResponseEnum responseEnum, Throwable e) {
        return error(responseEnum, e.getMessage() != null ? e.getMessage() : "系统异常，请联系管理员！");
    }
}
